package com.book.publishing.controller;

import com.book.buyout.entity.Vender;
import com.book.buyout.service.VenderService;
import com.book.common.annotation.ControllerEndpoint;
import com.book.common.controller.BaseController;
import com.book.common.entity.FebsConstant;
import com.book.common.entity.FebsResponse;
import com.book.common.utils.FebsUtil;
import com.book.common.utils.NameAndNumber;
import com.book.publishing.entity.Bookman;
import com.book.publishing.service.BookmanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 孙鑫
 * @Description:  20173952
 */

@Controller
public class RankingStatController extends BaseController {

    @Autowired
    BookmanService bookmanService;
    @Autowired
    VenderService venderService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "publishing/ranking")
    public String rankingStat() {
        return FebsUtil.view("publishing/ranking/rankingList");
    }


    /**
    * @Author: Mr.ZhangGaohe
    * @Date: 2021/4/10
    * @Description: 折线图
    */
    @GetMapping("ranking/line")
    @ResponseBody
    @ControllerEndpoint(operation = "折线图成功渲染", exceptionMessage = "折线图渲染失败")
    public FebsResponse getBreakageCount(Bookman bookman) {
        List<Bookman> bookmanList = bookmanService.findBookman(bookman);
        // 出版社名称
        String[] month = new String[bookmanList.size()];
        // 定义两种类型（销售、退回）
        List<String[]> countList = new ArrayList<>();
        // 销售
        String[] count1 = new String[bookmanList.size()];
        // 退回
        String[] count2 = new String[bookmanList.size()];
        // 得到出版社名称
        for (int i = 0; i < bookmanList.size(); i++) {
            month[i] = bookmanList.get(i).getPubname();
            count1[i] = String.valueOf(bookmanList.get(i).getSalenumber());
            count2[i] = String.valueOf(bookmanList.get(i).getBacknumber());
        }
        countList.add(month);
        countList.add(count1);
        countList.add(count2);
        return new FebsResponse().success().data(countList);
    }

    /**
    * @Author: Mr.ZhangGaohe
    * @Date: 2021/4/10
    * @Description: 饼型图
    */
    @GetMapping("vender/pctn")
    @ResponseBody
    @ControllerEndpoint(operation = "饼状图成功渲染", exceptionMessage = "饼状图渲染失败")
    public FebsResponse getBreakagePctn(Vender vender) {
        List<Vender> venderList = venderService.findVender(vender);
        List<NameAndNumber> countList = new ArrayList<>();
        for (int i = 0; i < venderList.size(); i++) {
            NameAndNumber nameAndNumber = new NameAndNumber();
            nameAndNumber.setName(venderList.get(i).getVendername()).setNumber(venderList.get(i).getTeamworknum());
            countList.add(nameAndNumber);
        }
        return new FebsResponse().success().data(countList);
    }

    @GetMapping("bookman/sale")
    @ResponseBody
    public FebsResponse saleSort() {
        return new FebsResponse().success().data(bookmanService.getSaleSort());
    }

    @GetMapping("bookman/back")
    @ResponseBody
    public FebsResponse backSort() {
        return new FebsResponse().success().data(bookmanService.getBackSort());
    }
}
